package com.budwk.app.sqfw.services.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.budwk.app.sqfw.component.CommunityStreetComponent;
import com.budwk.app.sqfw.models.SqfwDownFileRecordLog;
import com.budwk.app.sqfw.services.SqfwDownRecordlogService;
import com.budwk.app.sqfw.vo.SqfwDownFileRecordLogReqVo;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.starter.security.utils.SecurityUtil;
import org.apache.commons.lang3.StringUtils;
import org.nutz.aop.interceptor.async.Async;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@IocBean(args = {"refer:dao"})
public class SqfwDownFileRecordLogServiceImpl extends BaseServiceImpl<SqfwDownFileRecordLog> implements SqfwDownRecordlogService {
    public SqfwDownFileRecordLogServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private SysUserService sysUserService;

    @Async
    @Override
    public void insertRecordLog(String userId, String reqParam, String content, String title, String url, String entityId) {
        Sys_user user = sysUserService.fetch(userId);
        SqfwDownFileRecordLog log = new SqfwDownFileRecordLog();
        log.setCreateDate(LocalDateTime.now())
                .setCreateId(user.getId())
                .setCreateName(user.getUsername())
                .setReqParam(reqParam)
                .setContent(content)
                .setUrl(url)
                .setEntityId(entityId)
                .setTitle(title);
        this.insert(log);
    }

    @Override
    public Result<?> listByPage(SqfwDownFileRecordLogReqVo param) {
        Criteria cri = Cnd.cri();
        SqlExpressionGroup criSql = cri.where();
        if (StringUtils.isNotEmpty(param.getTitle())) {
            criSql.andLike(SqfwDownFileRecordLog::getTitle, param.getTitle());
        }
        if (StringUtils.isNotEmpty(param.getContent())) {
            criSql.andLike(SqfwDownFileRecordLog::getContent, param.getContent());
        }
        if (StringUtils.isNotEmpty(param.getCreateName())) {
            criSql.andLike(SqfwDownFileRecordLog::getCreateName, param.getCreateName());
        }
        if (param.getCreateStartDate() != null || param.getCreateEndDate() != null) {
            LocalDateTime startQueryTime = CommunityStreetComponent.handleStartQueryTime(param.getCreateStartDate());
            LocalDateTime endQueryTime = CommunityStreetComponent.handleStartQueryTime(param.getCreateEndDate());
            criSql.andBetween(SqfwDownFileRecordLog::getCreateDate, startQueryTime, endQueryTime);
        }
        cri.getOrderBy().desc(SqfwDownFileRecordLog::getCreateDate);
        Pagination pagination = this.listPage(param.getPageNo(), param.getPageSize(), cri);
        return Result.data(pagination);
    }

    @Override
    public Optional<String> getUrlByEntityIdAndTitle(String entityId, String title) {
        Criteria cri = Cnd.cri();
        SqlExpressionGroup criSql = cri.where();
        criSql.andEquals(SqfwDownFileRecordLog::getEntityId, entityId)
                .andEquals(SqfwDownFileRecordLog::getTitle, title);
        cri.getOrderBy().desc(SqfwDownFileRecordLog::getCreateDate);
        List<SqfwDownFileRecordLog> logList = this.query(cri);
        if (CollectionUtil.isEmpty(logList)) {
            return Optional.empty();
        }
        SqfwDownFileRecordLog log = logList.get(0);
        if (StringUtils.isNotEmpty(log.getUrl())) {
            return Optional.of(log.getUrl());
        }
        return Optional.empty();
    }

    @Override
    public SqfwDownFileRecordLog getUrlByEntityId(String entityId) {
        Cnd cnd = Cnd.NEW();
        SqlExpressionGroup criSql = cnd.where();
        criSql.andEquals(SqfwDownFileRecordLog::getEntityId, entityId)
                .andEquals(SqfwDownFileRecordLog::getCreateId, SecurityUtil.getUserId());
        cnd.getOrderBy().desc(SqfwDownFileRecordLog::getCreateDate);
        cnd.limit(1);
        return this.fetch(cnd);
    }
}
